package org.cloudie.docs.editor.client.widgets;

import org.cloudie.docs.editor.client.content.dialogs.SelectColorDialog;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusPanel;

public class ColorBox extends Composite {
	
  private String value;
  private FocusPanel panel;
	
  public ColorBox() {
    panel = new FocusPanel();
    panel.setTitle("Click to select color.");
    panel.setPixelSize(100, 18);
    panel.setStylePrimaryName("cloudie-ColorBox-Empty");
    panel.addClickHandler(new ClickHandler() {
	    @Override
		public void onClick(ClickEvent event) {
		  SelectColorDialog.get().center(new SelectionHandler<String>() {
			@Override
			public void onSelection(SelectionEvent<String> event) {
			  setValue(event.getSelectedItem());
			}
		  });
		}
    });
    initWidget(panel);
  }

  public String getValue() {
    return value;
  }

  public void setValue(String value) {
    this.value = value;
    if (value == null) {
      panel.setStylePrimaryName("cloudie-ColorBox-Empty");
      panel.getElement().getStyle().setBackgroundColor("");
    } else {
      panel.setStylePrimaryName("cloudie-ColorBox");
      panel.getElement().getStyle().setBackgroundColor(value);
    }
  }

}

